//
//  MenuContentViewController.m
//  ForengnMenu
//
//  Created by wenzhaoxu on 16/1/22.
//  Copyright © 2016年 余旺. All rights reserved.
//

#import "MenuContentViewController.h"
#import "ZWDHeaderView.h"
#import "HeaderView.h"
#import "MenuContentOneTableViewCell.h"
#import "MenuContentNOTableViewCell.h"
#import "ShiCaiCollectionViewCell.h"
#import "ExperienceTableViewCell.h"
#import "TipsTableViewCell.h"
#import <MediaPlayer/MediaPlayer.h>

@interface MenuContentViewController ()<ZWDHeaderViewDelegate,HeaderViewDelegate,UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@property (strong , nonatomic)ZWDHeaderView * headerView;

@property (strong , nonatomic)UIView * topView;

@property HeaderView * viewH;

@property (strong , nonatomic)NSArray * contents1;
@property (strong , nonatomic)NSArray * contents2;
@property (strong , nonatomic)NSArray * contents3;
@property (strong , nonatomic)NSArray * contents4;

@property int type;

@property BOOL isOpen;

@property BOOL isOk;

@property int praiseNumber;

@property BOOL  isShow;

@property UIButton * forwarBnt;

@property UIButton * collectionBnt;

@end

@implementation MenuContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.contents1 = self.recipeArray[@"Steps"];
    
    self.contents2 = self.recipeArray[@"Food"];
     
    self.contents3 = @[self.recipeArray[@"Tips"]];
    self.contents4 = @[self.recipeArray[@"ServiceTips"]];
    
    NSDictionary * dic = self.recipeArray[@"Content"];
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 45)];
    
    label.text = dic[@"RecipeName"];
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.textColor = [UIColor whiteColor];
    
    self.navigationItem.titleView = label;
 
    UIButton * rightBnt = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBnt.frame = CGRectMake(0, 0, 26, 30);
    [rightBnt setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [rightBnt addTarget:self action:@selector(returnAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBnt];
    
    if (_isEnter) {
        
        _forwarBnt = [UIButton buttonWithType:UIButtonTypeCustom];
        _forwarBnt.frame = CGRectMake(0, 0, 26, 30);
        
        [_forwarBnt setImage:[UIImage imageNamed:@"Icon_ forwarding"] forState:UIControlStateNormal];
        [_forwarBnt addTarget:self action:@selector(forwarding) forControlEvents:UIControlEventTouchUpInside];
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_forwarBnt];

        
    }else{
        
        if (_iskai) {
            _collectionBnt = [UIButton buttonWithType:UIButtonTypeCustom];
            
            _collectionBnt.frame = CGRectMake(0, 0, 26, 30);
            [_collectionBnt setImage:[UIImage imageNamed:@"Icon_ collection4"] forState:UIControlStateNormal];
            [_collectionBnt addTarget:self action:@selector(collection) forControlEvents:UIControlEventTouchUpInside];
            
            _forwarBnt = [UIButton buttonWithType:UIButtonTypeCustom];
            _forwarBnt.frame = CGRectMake(0, 0, 26, 30);
        }else{
            _collectionBnt = [UIButton buttonWithType:UIButtonTypeCustom];
            
            _collectionBnt.frame = CGRectMake(0, 0, 26, 30);
            [_collectionBnt setImage:[UIImage imageNamed:@"Icon_ collection3"] forState:UIControlStateNormal];
            [_collectionBnt addTarget:self action:@selector(collection) forControlEvents:UIControlEventTouchUpInside];
            
            _forwarBnt = [UIButton buttonWithType:UIButtonTypeCustom];
            _forwarBnt.frame = CGRectMake(0, 0, 26, 30);
        }
        
        
        [_forwarBnt setImage:[UIImage imageNamed:@"Icon_ forwarding"] forState:UIControlStateNormal];
        [_forwarBnt addTarget:self action:@selector(forwarding) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem * t1 = [[UIBarButtonItem alloc] initWithCustomView:_collectionBnt];
        UIBarButtonItem * t2 = [[UIBarButtonItem alloc] initWithCustomView:_forwarBnt];
        
        NSArray * a = @[t1,t2];
        
        self.navigationItem.rightBarButtonItems = a;
    }
    
    self.headerView = [[ZWDHeaderView alloc] initWithFrame:self.view.bounds];
    
    _viewH = [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil].lastObject;
    _viewH.delegate =self;
    self.headerView.headerView = _viewH;
    
    _viewH.headImageView.image = [UIImage imageNamed:dic[@"RecipeImage"]];
    if (!_viewH.headImageView.image) {
        _viewH.headImageView.image = dic[@"RecipeImageData"];
    }
    _viewH.recipeName.text = dic[@"RecipeName"];
    _viewH.recipeShow.text = dic[@"RecipeShow"];
    _viewH.cookingDifficulty.text = dic[@"CookingDifficulty"];
    _viewH.cookingTime.text = dic[@"CookingTime"];
    _viewH.collectionTaste.text = dic[@"CollectionTaste"];
    _viewH.praiseNumber.text = dic[@"Praise"];
    
    NSString * string = [NSString stringWithFormat:@"%@",dic[@"Praise"]];
    
    self.praiseNumber = [string intValue];
    
    UITableView * view1 = [UITableView new];
    view1.frame = self.view.bounds;
    view1.tag =0;
    view1.dataSource = self;
    view1.delegate = self;
    view1.separatorStyle = UITableViewCellStyleDefault;
    //设置Cell 的行高为自动适应尺寸
    view1.rowHeight = UITableViewAutomaticDimension;
    //Cell的高度预估值
    view1.estimatedRowHeight = 120;

    
    UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new];
    UICollectionView * view2 = [[UICollectionView alloc] initWithFrame:CGRectMake(320, 0, 320, 568) collectionViewLayout:layout];
    
    UINib * nib = [UINib nibWithNibName:@"ShiCaiCollectionViewCell" bundle:[NSBundle mainBundle]];
    [view2 registerNib:nib forCellWithReuseIdentifier:@"ShiCaiCollectionViewCell"];
    
    view2.backgroundColor =[UIColor colorWithRed:246/255.0f green:247/255.0f blue:239/255.0f alpha:1.0f];
    view2.dataSource = self;
    view2.tag =1;
    view2.delegate = self;
    
    UITableView * view3 = [UITableView new];
    view3.frame = CGRectMake(320*2, 0, 320, 568);
    view3.dataSource = self;
    view3.tag = 2;
    view3.delegate=self;
    
    UITableView * view4= [UITableView new];
    view4.frame = CGRectMake(320*3, 0, 320, 568);
    view4.dataSource =self;
    view4.tag = 3;
    view4.delegate = self;
    
    self.headerView.subScrollViews = @[view1,view2,view3,view4];
    [self.view addSubview:self.headerView];
    _headerView.sectionHeight = 64.0f;
    self.headerView.delegate  = self;
    
}


- (NSArray *)contents:(NSInteger)type{

    switch (type) {
        case 0:
            return self.contents1;
            
            break;
        case 1:
            
            return self.contents2;
            break;
        case 2:
            
            return self.contents3;
            break;

        default:
            return self.contents4;
            
            break;
    }
}

-(void)returnAction{

    [self.navigationController popViewControllerAnimated:YES];
}
//收藏
-(void)collection{
    
    //警告视图
    UIAlertController * alertCtr = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            [_collectionBnt setImage:[UIImage imageNamed:@"Icon_ collection4"] forState:UIControlStateNormal];
            _isShow =YES;
    }];
    
    UIAlertAction *towAction = [UIAlertAction actionWithTitle:@"取消收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [_collectionBnt setImage:[UIImage imageNamed:@"Icon_ collection3"] forState:UIControlStateNormal];
        
        if (_iskai) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        _isShow =NO;
 
    }];
    
    UIAlertAction *thirdAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
    }];
    [alertCtr addAction:firstAction];
    
    [alertCtr addAction:towAction];
    
    [alertCtr addAction:thirdAction];
    
    [self presentViewController:alertCtr animated:YES completion:^{}];

}
//分享
-(void)forwarding{
    //警告视图
    UIAlertController * alertCtr = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"分享到生活圈" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }];
    
    UIAlertAction *thirdAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
    }];
    [alertCtr addAction:firstAction];
    
    [alertCtr addAction:thirdAction];
    
    [self presentViewController:alertCtr animated:YES completion:^{}];

    
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [UIView new];
        _topView.frame = CGRectMake(0, -20, 320, 64);
        _topView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
        _topView.userInteractionEnabled = NO;
    }
    
    return _topView;
}

//当界面消失之前调用
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    //让导航栏透明
    UIImage *image = [UIImage imageNamed:@"Icon_white_background"];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    //让分割线透明
    [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@""]];
    
    [self.topView removeFromSuperview];
}

//视图将要出现时
-(void)viewWillAppear:(BOOL)animated{
    //让导航栏透明
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    
    
    [self.navigationController.navigationBar insertSubview:self.topView atIndex:0];
    
    //让分割线透明
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray * contents =  [self contents:collectionView.tag];
    
    return contents.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"ShiCaiCollectionViewCell";

    ShiCaiCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    NSArray * contents = [self contents:collectionView.tag];
    
    NSDictionary * info = contents[indexPath.row];

    cell.shiPingImageView.image = [UIImage imageNamed:info[@"foodImage"]];
    
    cell.numberLabel.text = info[@"foodDosage"];
    
    cell.name.text = info[@"foofName"];
    
    return cell;
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray * contents =  [self contents:tableView.tag];
    
    return contents.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSArray * contents = [self contents:tableView.tag];
    
    if (tableView.tag==0) {
        static NSString * identifier = @"Cell";

        NSDictionary * info = contents[indexPath.row];
        
        if ([info[@"imageViewName"] isEqualToString:@" "]) {
            
            MenuContentNOTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell== nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"MenuContentNOTableViewCell" owner:self options:nil]lastObject];
                
            }
        
            cell.numberLabel.text = info[@"NuberLabel"];
            cell.myTextLabel.text = info[@"StepsLabel"];
            cell.backgroundColor =[UIColor colorWithRed:246/255.0f green:247/255.0f blue:239/255.0f alpha:1.0f];
            return cell;
        }else{
            
            MenuContentOneTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            
            if (cell== nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"MenuContentOneTableViewCell" owner:self options:nil]lastObject];
            }

            cell.myImageView.image = [UIImage imageNamed:info[@"imageViewName"]];
            if (!cell.myImageView.image) {
                cell.myImageView.image = info[@"imageData"];
            }
            
            cell.myNumber.text = info[@"NuberLabel"];
            cell.myTextLable.text = info[@"StepsLabel"];
            cell.backgroundColor = [UIColor colorWithRed:246/255.0f green:247/255.0f blue:239/255.0f alpha:1.0f];
            return cell;
        }
        
    }else if(tableView.tag==1){
        static NSString * identifier = @"Cell";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell== nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        
        cell.backgroundColor =[UIColor colorWithRed:246/255.0f green:247/255.0f blue:239/255.0f alpha:1.0f];
        return cell;
    }else if(tableView.tag==2){
        static NSString * identifier = @"Cell";
        ExperienceTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell== nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ExperienceTableViewCell" owner:self options:nil]firstObject];
        }

        NSArray * contents = [self contents:tableView.tag];
        
        cell.myTextLabel.text = contents[indexPath.row];
        
        cell.backgroundColor =[UIColor colorWithRed:246/255.0f green:247/255.0f blue:239/255.0f alpha:1.0f];
        return cell;
    }else{
        static NSString * identifier = @"Cell";
        TipsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell== nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"TipsTableViewCell" owner:self options:nil]lastObject];
        }
        
        NSArray * contents = [self contents:tableView.tag];
        cell.myTipsLabel.text = contents[indexPath.row];
        cell.backgroundColor =[UIColor colorWithRed:246/255.0f green:247/255.0f blue:239/255.0f alpha:1.0f];
        return cell;
    }
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSArray * contents = [self contents:tableView.tag];
    
    if (tableView.tag==0) {
        NSDictionary * info = contents[indexPath.row];
        if ([info[@"imageViewName"] isEqualToString:@" "]) {
            
            return 44;
        }else{
            return 243;
        }
    }else if(tableView.tag==1){
        return 44;
    }else if(tableView.tag==2){
        
        return 145;
    }else{
    
        return 320;
    }
    
}


- (void)scrollEndingWithZwdView:(ZWDHeaderView *)zwdView withIndex:(NSInteger)index{

    if (index==0) {
        [UIView animateWithDuration:0.5 animations:^{
            CGRect rect = _viewH.myView.frame;
            rect.origin.x = 0;
            _viewH.myView.frame  =rect;
        }];
    }else if(index==1){
        [UIView animateWithDuration:0.5 animations:^{
            CGRect rect = _viewH.myView.frame;
            rect.origin.x = rect.size.width;
            _viewH.myView.frame  =rect;
        }];
    }else if(index==2){
        [UIView animateWithDuration:0.5 animations:^{
            CGRect rect = _viewH.myView.frame;
            rect.origin.x = rect.size.width + 80;
            _viewH.myView.frame  =rect;
        }];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            CGRect rect = _viewH.myView.frame;
            rect.origin.x = rect.size.width + 80*2;
            _viewH.myView.frame  =rect;
        }];
    }
    
}

-(void)selectAction:(NSInteger)index{
    if (index==0) {
        [UIView animateWithDuration:0.5 animations:^{
            CGRect rect = _viewH.myView.frame;
            rect.origin.x = 0;
            _viewH.myView.frame  =rect;
        }];
    }else if(index==1){
        [UIView animateWithDuration:0.5 animations:^{
            CGRect rect = _viewH.myView.frame;
            rect.origin.x = rect.size.width;
            _viewH.myView.frame  =rect;
        }];
    }else if(index==2){
        [UIView animateWithDuration:0.5 animations:^{
            CGRect rect = _viewH.myView.frame;
            rect.origin.x = rect.size.width + 80;
            _viewH.myView.frame  =rect;
        }];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            CGRect rect = _viewH.myView.frame;
            rect.origin.x = rect.size.width + 80*2;
            _viewH.myView.frame  =rect;
        }];
    }    [self.headerView setSelectedIndex:index withAnimation:YES];
}

-(void)headImageViewAction{

    NSDictionary * dic = self.recipeArray[@"Content"];
    
    NSString * string = dic[@"VideoLink"];
    
    MPMoviePlayerViewController *movie = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:string]];//URLWithString

    [movie.moviePlayer prepareToPlay];
    
    [self presentMoviePlayerViewControllerAnimated:movie];


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//返回每一个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    

    return CGSizeMake(145, 145);
}
//设置边界间隙
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    //上、左、下、右
    return UIEdgeInsetsMake(0, 10, 10, 10);
}
//赞
-(void)praiseAction{

    if (_isOk) {
        [_viewH.praiseBnt setImage:[UIImage imageNamed:@"Icon_praiseNO.png"] forState:UIControlStateNormal];
        _praiseNumber =_praiseNumber-1;
        
        _viewH.praiseNumber.text = [NSString stringWithFormat:@"%d",_praiseNumber];
        
        _isOk = NO;
    }else{
        [_viewH.praiseBnt setImage:[UIImage imageNamed:@"Icon_praiseYES.png"] forState:UIControlStateNormal];
        _praiseNumber =_praiseNumber+1;
        
        _viewH.praiseNumber.text = [NSString stringWithFormat:@"%d",_praiseNumber];
        _isOk =YES;
    }

    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    float alpha = scrollView.contentOffset.y/225.0f + 1.8;
    
    self.topView.alpha = alpha;
    
}

@end
